/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PedidosNaSemanaForm.java
 *
 * Created on 26/10/2013, 08:56:13
 */
package Formularios;

import Classes.ClientesClass;
import Classes.ProdutoPSemanaClass;
import DAO.ProdutoPSemanaDAO;
import java.awt.Color;
import java.text.DateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

        

/**
 *
 * @author Carlos Schaefer
 */
public class PedidosNaSemanaForm extends javax.swing.JFrame {
    private int semana;
    private int ano;
    private String Data_Incial;

    /** Creates new form PedidosNaSemanaForm */
    public PedidosNaSemanaForm() {
        initComponents();
        setLocationRelativeTo(null);
        getContentPane().setBackground(Color.WHITE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblSemana = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        tblSemana.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Código", "Ano", "Semana", "Data Inicial", "Data Final", "Qtd Pedidos", "Qtd na Previsão"
            }
        ));
        tblSemana.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSemanaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblSemana);
        tblSemana.getColumnModel().getColumn(0).setMinWidth(0);
        tblSemana.getColumnModel().getColumn(0).setPreferredWidth(0);
        tblSemana.getColumnModel().getColumn(0).setMaxWidth(0);

        jButton1.setText("Editar Semana");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Zerar Previsão da semana");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Pedidos na Semana");

        jButton3.setText("Incluir Pedido/Previsão");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 597, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
     
    if (ano != 0){
    
    EdicaoPedido form = new EdicaoPedido();
    form.setAno(ano);
    form.setSemana(semana);
    form.setVisible(true);
    form.setDefaultCloseOperation(HIDE_ON_CLOSE);  
    } else {
        JOptionPane.showMessageDialog(null, "Não há registro selecionado!!\nSelecione um registro!");
    }
    
    
    
    
    
    
}//GEN-LAST:event_jButton1ActionPerformed

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
     Listar();
}//GEN-LAST:event_formWindowOpened

private void tblSemanaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSemanaMouseClicked

     int linha = tblSemana.getSelectedRow();
     int codigo = 0;
     
     
     ano = Integer.parseInt(String.valueOf(tblSemana.getValueAt(linha, 1)));
     semana = Integer.parseInt(String.valueOf(tblSemana.getValueAt(linha, 2)));
     
    
    
    
    
}//GEN-LAST:event_tblSemanaMouseClicked

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    dao.zerarpedidosemana(semana, ano);
    Listar();
    
    
    
    
    
    
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
   PedidoPrevisaoDialogForm pedidoPrevisaoDaialog = new PedidoPrevisaoDialogForm(this, true);
   pedidoPrevisaoDaialog.setModo(1);
   pedidoPrevisaoDaialog.setVisible(true);
}//GEN-LAST:event_jButton3ActionPerformed

private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained

}//GEN-LAST:event_formFocusGained

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
Listar();
}//GEN-LAST:event_formWindowGainedFocus

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PedidosNaSemanaForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PedidosNaSemanaForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PedidosNaSemanaForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PedidosNaSemanaForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PedidosNaSemanaForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblSemana;
    // End of variables declaration//GEN-END:variables
    public ProdutoPSemanaClass produtSem = new ProdutoPSemanaClass(); 
    public ProdutoPSemanaDAO dao = new ProdutoPSemanaDAO();
    public ClientesClass mdao = new ClientesClass();
    
    private void Listar() {
        
        
        
        
        
        
        DefaultTableModel model  = (DefaultTableModel) tblSemana.getModel();
        model.setNumRows(0);        

        for (ProdutoPSemanaClass ProdSem : dao.ListarAgrupado()) {
            model.addRow(new String[]{
                        "" + ProdSem.getcodigo(),
                        "" + ProdSem.getAno(),
                        "" + ProdSem.getNumerosemana(),
                        "" + Data_Inicial(ProdSem.getNumerosemana(), ProdSem.getAno()),
                        "" + Data_final(ProdSem.getNumerosemana(), ProdSem.getAno()),
                        "" + dao.retornaQtdPedSem(ProdSem.getNumerosemana()),
                        "" + dao.retornaQtdPrevSem(ProdSem.getNumerosemana())
                     
                    
                    
                    
                    });
        }
        
        
        
        
        
        
        
        
    }


    private String Data_Inicial(int semana, int ano) {
        
        //Date dt_inicial= new Date();
        
        Calendar c = Calendar.getInstance();
        c.set(ano,00,01);
        c.add(Calendar.WEEK_OF_YEAR,semana );
        c.add(Calendar.DAY_OF_YEAR,-1 );
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM  );
        
        return (df.format(c.getTime()));
        
    }
    
    
    private String Data_final(int semana, int ano) {
        
        //Date dt_inicial= new Date();
        
        Calendar c = Calendar.getInstance();
        c.set(ano,00,01);
        c.add(Calendar.WEEK_OF_YEAR,semana+1 );
        c.add(Calendar.DAY_OF_YEAR,-1 );
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM  );
        
        return (df.format(c.getTime()));
        
    }
    
    
    
    
    
    
    
}
